package day11.console.impl;

import day11.console.ConsoleCommand;
import day11.packet.request.MessageRequestPacket;
import io.netty.channel.Channel;

import java.util.Scanner;

/**
 * @author chenshijie
 * @date 2024/1/1 14:32
 */

public class SendToUserConsoleCommand implements ConsoleCommand {
    @Override
    public void exec(Scanner sc, Channel channel) {
        System.out.println("输入你想发送消息的用户");
        String toUserId = sc.nextLine();
        if (!isNumeric(toUserId)) {
            System.err.println("输入账号有误");
        } else {
            System.out.println("输入消息");
            String msg = sc.nextLine();
            MessageRequestPacket messageRequestPacket = new MessageRequestPacket();
            messageRequestPacket.setMessage(msg);
            messageRequestPacket.setToUserId(Integer.parseInt(toUserId));

            channel.writeAndFlush(messageRequestPacket);

            System.out.println("发送成功");
        }
    }

    public static boolean isNumeric(String str){
        if (str.length() != 9) {
            return false;
        }
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
}
